<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class admin_elibrary extends CI_Controller {
    private $limit = 20;
    function __construct() {
        parent::__construct();
        $this->load->model('model_elibrary', 'm_elib', TRUE);
    }
    
    function index($offset = 0, $order_column = 'datecreated', $order_type = 'DESC') {
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        else{
            
            $data = array(
                'web_title' =>  'Admin Elibrary | List data',
                'main_view' =>  'mimin/elibrary/index_new',
                'list_data' =>  $this->m_elib->get_all(),
            );
            $this->load->view('mimin/template/template_table',$data);
        }
    }
    function add(){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        else{
            $this->form_validation->set_rules('judul','Judul','required');
            $this->form_validation->set_rules('content','Isi Konten','required');
            $this->form_validation->set_rules('kategori','Kategori','required');
            if($this->form_validation->run() == FALSE){
                // set common properties
                $data = array(
                    'web_title'             =>  'Data Elibrary Baru',
                    'msg_error_username'    =>  form_error('judul','<caption>','</caption>'),
                    'msg_error_name'        =>  form_error('content','<caption>','</caption>'),
                    'main_view'             =>  "mimin/elibrary/add"
                );
                $this->parser->parse("mimin/template/template_editor",$data);
            }
            else{
                $upload_path = "";
                if(strtolower($this->input->post('kategori')) == "dasarhukum"){
                    $config['upload_path'] = './content/elibrary/dasarhukum/';
                    $upload_path = 'content/elibrary/dasarhukum/';
                }
                elseif(strtolower($this->input->post('kategori')) == "info"){
                    $config['upload_path'] = './content/elibrary/info/';
                    $upload_path = 'content/elibrary/info/';
                }
                elseif(strtolower($this->input->post('kategori')) == "report"){
                    $config['upload_path'] = './content/elibrary/report/';
                    $upload_path = 'content/elibrary/report/';
                }
                
                $config['allowed_types'] = 'pdf|doc|docx|zip|rar|ppt|pptx';
                $this->upload->initialize($config);     
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload("userfile")){
                    $data = array(
                        "main_view"     =>  "mimin/elibrary/add",
                        "web_title"     =>  "Upload New News Data",
                        "error"         =>  $this->upload->display_errors('','')
                    );
                    $this->load->view("mimin/template/template_editor",$data);
                }
                else{
                    date_default_timezone_set("Asia/Jakarta");
                    $curdate = date("Y-m-d h:m:s");
                    $upload_data = $this->upload->data();
                    /*
                     * upload image cover
                     */
                    $configa['upload_path'] = './content/elibrary/cover/';
                    $cover_path = 'content/elibrary/cover/';
                    $configa['allowed_types'] = 'jpg|png|jpeg';
                    $this->upload->initialize($configa);     
                    if (!$this->upload->do_upload("imageinput")){
//                        exit("sini");
                        $data = array(
                            "main_view"     =>  "mimin/elibrary/add",
                            "web_title"     =>  "Upload New News Data",
                            "error"         =>  $this->upload->display_errors('a','')
                        );
                        $this->load->view("mimin/template/template_editor",$data);
                        
                    }
//
                    $upload_image = $this->upload->data();
                    //save data elibrary
                    $isi = array(
                        'judul'         =>  $this->input->post('judul'),
                        'deskripsi'     =>  $this->input->post('content'),
                        'file'          =>  $upload_path.$upload_data['file_name'],
                        'image'         =>  $cover_path.$upload_image['file_name'],
                        'kategori'      =>  strtolower($this->input->post('kategori')),
                        'datecreated'   =>  $curdate,
                        'datemodified'  =>  $curdate,
                        'createdby'     =>  $this->session->userdata('admin_user')->id_admin,
                        'modifiedby'    =>  $this->session->userdata('admin_user')->id_admin
                    );
                    if($this->m_elib->save($isi)){
                        $data = array(
                            "main_view"         =>  "mimin/elibrary/add",
                            "web_title"         =>  "Upload New News Data",
                            "success_status"    =>  1
                        );
                        $this->load->view("mimin/template/template_editor",$data);
                    }
                }
            }
        }
    }
    
    function add_act(){
        $headers = apache_request_headers();
        $is_ajax = (isset($headers['X-Requested-With']) && $headers['X-Requested-With'] == 'XMLHttpRequest');
        if($is_ajax){
            $this->form_validation->set_rules('judul','Judul','required');
            $this->form_validation->set_rules('content','Isi Konten','required');
            $this->form_validation->set_rules('kategori','Kategori','required');
            if($this->form_validation->run() == FALSE){
                print_r($_POST);
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  validation_errors('',''),
                );
                exit(json_encode($output));
            }
            $upload_path = "";
            if(strtolower($this->input->post('kategori')) == "dasarhukum"){
                $config['upload_path'] = './content/elibrary/dasarhukum/';
                $upload_path = '/content/elibrary/dasarhukum/';
            }
            elseif(strtolower($this->input->post('kategori')) == "info"){
                $config['upload_path'] = './content/elibrary/info/';
                $upload_path = '/content/elibrary/info/';
            }
            elseif(strtolower($this->input->post('kategori')) == "report"){
                $config['upload_path'] = './content/elibrary/report/';
                $upload_path = '/content/elibrary/report/';
            }
            $config['allowed_types'] = 'pdf|doc|docx|zip|rar|ppt|pptx';

            $this->load->library('upload', $config);
            if (!$this->upload->do_upload("userfile")){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  $this->upload->display_errors('',''),
                );
                exit(json_encode($output));
            }
            $output = array(
                "status"    =>  0,
                "msg"       =>  "ok"
            );
            exit(json_encode($output));
        }
        else{
            die("Access denied");
        }
    }
    
    function delete($id=""){
//        print_r($this->session->userdata('admin_user'));exit();
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($id=="" || $this->session->userdata('admin_user')->level != 1){
            redirect("admin_home","refresh");
        }
        else{
            $list_data = $this->m_elib->select_by_id($id);
            $path=  base_url()."";
            foreach ($list_data as $row) {
                $path = $row->file;
            }
            if($this->m_elib->delete($id)){ 
                redirect("admin_elibrary","refresh");
            }
            else{
                echo"Cannot Delete Data, Error :".  mysql_error();
            }
        }
    }
    function detail($id=""){
        //SET TIME ZONE
        date_default_timezone_set("Asia/Jakarta");
        $curdate = date("Y-m-d h:i:s");
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($id==""){
            redirect("admin_news","refresh");
        }
        else{
            $count = $this->m_elib->select_by_id($id,"count");
            if($count > 0){
                $this->form_validation->set_rules('judul','Judul','required');
                $this->form_validation->set_rules('content','Isi Konten','required');
                $this->form_validation->set_rules('kategori','Kategori','required');
                if($this->form_validation->run() == FALSE){
                    $data = array(
                        "main_view"     =>  "mimin/elibrary/detail",
                        "web_title"     =>  "E Library Detail",
                        "list_data"     =>  $this->m_elib->select_by_id($id)
                    );
                    $this->load->view("mimin/template/template_editor",$data);
                }
                else{
                    //jika dia pilih file
                    if($this->input->post("new_value")){
                        $upload_path = "";
                        if(strtolower($this->input->post('kategori')) == "dasarhukum"){
                            $config['upload_path'] = './content/elibrary/dasarhukum/';
                            $upload_path = 'content/elibrary/dasarhukum/';
                        }
                        elseif(strtolower($this->input->post('kategori')) == "info"){
                            $config['upload_path'] = './content/elibrary/info/';
                            $upload_path = 'content/elibrary/info/';
                        }
                        elseif(strtolower($this->input->post('kategori')) == "report"){
                            $config['upload_path'] = './content/elibrary/report/';
                            $upload_path = 'content/elibrary/report/';
                        }
                        $config['allowed_types'] = 'pdf|doc|docx|zip|rar|ppt|pptx';
                        $this->load->library('upload', $config);
                        //IF THE ADMIN Do Not Choose any image to upload
                        if (!$this->upload->do_upload()){
                            
                            $data = array(
                                "main_view"         =>  "mimin/elibrary/detail",
                                "web_title"         =>  "Error Update Elibrary Data",
                                "error"             =>  $this->upload->display_errors('',''),
                                "list_data"         =>  $this->m_elib->select_by_id($id)
                            );
                            $this->load->view("mimin/template/template_editor",$data);
                        }
                        //jika berhasil upload
                        else{
                            $upload_data = $this->upload->data();
                            $isi_update = array(
                                'judul'         =>  $this->input->post('judul'),
                                'deskripsi'     =>  $this->input->post('content'),
                                'file'          =>  $upload_path.$upload_data['file_name'],
                                'kategori'      =>  strtolower($this->input->post('kategori')),
                                'datemodified'  =>  $curdate,
                                'modifiedby'    =>  $this->session->userdata('admin_user')->id_admin
                            );
                            $status_update = $this->m_elib->update($id,$isi_update);
                            if($status_update == false){
                                echo"false";
                            }
                            else{
                                $data = array(
                                    "main_view"         =>  "mimin/elibrary/detail",
                                    "web_title"         =>  "Successfull Update Elibrary Data",
                                    "success_status"    =>  1,
                                    "list_data"     =>  $this->m_elib->select_by_id($id)
                                );
                                $this->load->view("mimin/template/template_editor",$data);
                            }
                        }
                    }
                    else{
                        $isi_update = array(
                            'judul'         =>  $this->input->post('judul'),
                            'deskripsi'     =>  $this->input->post('content'),
                            'kategori'      =>  strtolower($this->input->post('kategori')),
                            'datemodified'  =>  $curdate,
                            'modifiedby'    =>  $this->session->userdata('admin_user')->id_admin
                        );
                        $status_update = $this->m_elib->update($id,$isi_update);
                        if($status_update){
                            $data = array(
                                "main_view"         =>  "mimin/elibrary/detail",
                                "web_title"         =>  "Successfull Update Elibrary Data",
                                "success_status"    =>  1,
                                "list_data"         =>  $this->m_elib->select_by_id($id)
                            );
                            $this->load->view("mimin/template/template_editor",$data);
                        }
                        else{
                            echo"Cannot update data";
                        }
                    }
                }
            }
            else{
                redirect("admin_news","refresh");
            }
        }
    }
}

/* End of file admin_news.php */
/* Location: ./application/controllers/admin_event.php */